package org.example.Hashtable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class Sum_of_three_numbers {
    public static void main(String[] args) {
        //三数之和

        //给你一个整数数组 nums ，判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != j、i != k 且 j != k ，同时还满足 nums[i] + nums[j] + nums[k] == 0 。请
        //你返回所有和为 0 且不重复的三元组。
        //注意：答案中不可以包含重复的三元组。
        int[] nums = {-1,0,1,2,-1,-4};
        //int[] nums = {0,0,0,0};
        List<List<Integer>> arrayList = function2(nums);
        for (List<Integer> list : arrayList) {
            for (Integer i : list) {
                System.out.print(i+" ");
            }
            System.out.println();
        }
    }
    //双指针法
    public static List<List<Integer>> function2(int[] nums){
        Arrays.sort(nums);
        ArrayList<List<Integer>> arrayList = new ArrayList<>();
        for (int i = 0; i < nums.length-2; i++) {
            if (nums[i]>0){
                return arrayList;
            }
            int left = i+1;
            int right = nums.length-1;
            if (i!=0 && nums[i-1] == nums[i]){
                continue;
            }
            while(left < right){
                if (nums[i] + nums[left] + nums[right] == 0){
                    ArrayList<Integer> list = new ArrayList<>();
                    list.add(nums[i]);
                    list.add(nums[left]);
                    list.add(nums[right]);
                    arrayList.add(list);
                    while(left < right && nums[left]==nums[left+1]){
                        left++;
                    }
                    while(left < right && nums[right-1]==nums[right]){
                        right--;
                    }
                    left++;
                    right--;
                }else if (nums[i] + nums[left] + nums[right] < 0){
                    left++;
                }else {
                    right--;
                }
            }
        }
        return arrayList;
    }
    //哈希法，超出时间限制，真他妈操蛋
    public static List<List<Integer>> function1(int[] nums){
        for (int i = 0; i <nums.length; i++) {
            for (int j = 0; j < nums.length-i-1; j++) {
                if (nums[j]>nums[j+1]){
                    int tem = nums[j];
                    nums[j] = nums[j+1];
                    nums[j+1] = tem;
                }
            }
        }
        ArrayList<List<Integer>> arrayList = new ArrayList<>();
        for (int i = 0; i < nums.length-2; i++) {
            if (i != 0 && nums[i-1] == nums[i]){
                continue;
            }
            for (int j = i+1; j < nums.length-1; j++) {
                if (j !=i+1 && nums[j-1] == nums[j]){
                    continue;
                }
                HashSet<Integer> hashSet = new HashSet<>();
                for (int k = j+1; k < nums.length; k++) {
                    hashSet.add(nums[k]);
                }
                if (hashSet.contains(0-nums[i]-nums[j])){
                    ArrayList<Integer> list = new ArrayList<>();
                    list.add(nums[i]);
                    list.add(nums[j]);
                    list.add(0-nums[i]-nums[j]);
                    arrayList.add(list);
                }
            }
        }
        return arrayList;
    }
}
